import { Loading } from 'element-ui';

const options = {
    customClass: 'loading-blur-class',
    background : 'rgba(0,0,0,0)',
    text       : '加载中',
    name       : 'default',
    lock       : true,
    fullscreen : false,
};

export default {
    data() {
        return {
            list: {},
        }
    },
    methods: {
        showLoading(opt = {}) {
            opt        = Object.assign({}, options, opt);
            const name = opt.name;
            if (this.list[ name ]) {
                return;
            }

            opt.target = opt.target || this.$refs.pageContainer;
            if (!opt.target) {
                return;
            }

            const loading = Loading.service(opt);

            this.$set(this.list, name, loading);
        },
        closeLoading(name = 'default') {
            const loading = this.list[ name ];
            if (!loading) {
                return;
            }
            this.$nextTick(() => {
                loading.close();
                this.$set(this.list, name, null);
            });
        },
    }
}
